<?php
/**
 * Created by PhpStorm.
 * User: tandunzhao
 * Date: 2018/5/30
 * Time: 下午1:47
 */

namespace App\Modules\Shop\Services;


use App\Modules\Shop\Models\Products;

class ProductsService
{
    /**
     * 获取商品列表
     * @param array $params
     * @param bool $is_all
     * @return mixed
     */
    public static function getProductsList(Array $params=[],$is_all = false) {
        $obj = Products::where('status',1)->orderBy('id')->select(['id','product_name','img_thum','category_id','paid_num']);
        if(isset($params['product_name']) && $params['product_name']) {
            $obj->where('product_name','like','%'.$params['product_name'].'%');
        }
        if(isset($params['category_id']) && $params['category_id']) {
            $obj->where('category_id',$params['category_id']);
        }
        if($is_all){
            return $obj->get();
        }
        return $obj->paginate();
    }

    public static function getProductsDetail($id) {
        return Products::where('id',$id)->where('status',1)->first();
    }

    public static function getProductsDetailAll($id) {
        return Products::where('id',$id)->where('status','>=',0)->first();
    }

    public static function getGoodsListByids($ids) {
        return Products::whereIn('id',$ids)->where('status',1)->get();
    }

    public static function addProduct($data) {
        return Products::addProduct($data);
    }

    public static function updateProduct($id,$data) {
        return Products::updateProduct($id,$data);
    }

    public static function getProductsListDetail($params,$is_all=false) {
        $obj = Products::where('status','>=',0)->orderBy('id','desc');
        if(isset($params['goods_name']) && $params['goods_name']) {
            $obj->where('goods_name','like','%'.$params['goods_name'].'%');
        }
        if(isset($params['category_id']) && $params['category_id']) {
            $obj->where('category_id',$params['category_id']);
        }
        if($is_all) {
            return $obj->get();
        }
        return $obj->paginate();
    }

    public static function deleteProducts($id) {
        return Products::deleteProducts($id);
    }
}